#include "Stdafx.h"
#include "Frame.h"

bool CFrame::Create()
{
	WNDCLASSA wndClass;
	wndClass.cbClsExtra = 0;
	wndClass.cbWndExtra = 0;
	wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndClass.hCursor = LoadCursorA(NULL, IDC_ARROW);
	wndClass.hIcon = LoadIconA(NULL, IDI_APPLICATION);
	wndClass.hInstance = (HINSTANCE)GetModuleHandle(NULL);
	wndClass.lpfnWndProc = WndProc;
	wndClass.lpszClassName = "CFrame";
	wndClass.lpszMenuName = NULL;
	wndClass.style = CS_HREDRAW | CS_VREDRAW;
	if(RegisterClassA(&wndClass) == 0)
		return false;
	
	int weight = GetSystemMetrics(SM_CXSCREEN);
	int height = GetSystemMetrics(SM_CYSCREEN);
	DWORD wndStyle = WS_OVERLAPPED | WS_CAPTION | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME | WS_SYSMENU;
	m_hWnd = CreateWindowA("CFrame", "MainFrame", wndStyle, weight / 4, height / 4, weight / 2, height / 2, NULL, NULL, (HINSTANCE)GetModuleHandle(NULL), NULL);
	return m_hWnd == NULL ? false : true;
}

LRESULT __stdcall CFrame::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_CLOSE:
		PostQuitMessage(0);
		break;
	case WM_KEYUP:
		if(wParam == VK_ESCAPE)
			PostQuitMessage(0);
		break;
	}
	return DefWindowProcA(hWnd, msg, wParam, lParam);
}

void CMsgLoop::Enter(int interval)
{
	bool bStop = false;
	MSG msg;
	while(!bStop)
	{
		LONGLONG start = time_ms();
		while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
				bStop = true;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		int consumeTime = time_ms() - start;
		if(consumeTime < interval)
			Sleep(interval - consumeTime);		
	}
}

CButton::CButton(const char* pName):m_name(pName)
{
}

bool CButton::Create(HWND parent)
{
	HWND button = CreateWindowA("BUTTON", m_name.c_str(), BS_DEFPUSHBUTTON | WS_CHILD, 100, 100, 500, 150, parent, NULL, (HINSTANCE)GetModuleHandleA(NULL), NULL);
	ShowWindow(button, SW_SHOW);
	return true;
}
